package net.ciklum.svnnotify.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;

 
public class DataHelper {
 
   private static final String SVNDB = "svn.db";
   private static final int DATABASE_VERSION = 4;
   private static final String SVN_TABLE = "mysvn";
   private static final String SVN_NAME = "name";
   private static final String SVN_URL = "url";
   private static final String SVN_LOGIN = "login";
   private static final String SVN_PASSWORD = "pwd";
   private static final String ID = "id";
   private static final String NOTIFI_TABLE = "mynotification";
   private static final String NOTIFI_REV_ID = "revision_id";
   private static final String NOTIFI_AUTHOR = "author";
   private static final String NOTIFI_REV_DATE = "revision_date";
   private static final String NOTIFI_MESSAGE = "message";
   private static final String NOTIFI_CHENGED_PATH = "changed_paths";
 
 
   private Context context;
   private SQLiteDatabase db;
   private SQLiteStatement insertStmt;
   
   private static final String INSERT_SVN = "insert into "
      + SVN_TABLE + "(" + SVN_NAME + " , " + SVN_URL + "," 
	  + SVN_LOGIN + "," + SVN_PASSWORD + ") values (?,?,?,?)";
   
   private static final String INSERT_NOTIFI = "insert into "
	  + NOTIFI_TABLE + "(" + NOTIFI_REV_ID + " , "+ NOTIFI_AUTHOR + " , " 
	  + NOTIFI_REV_DATE + " , "+ NOTIFI_MESSAGE + " , " + NOTIFI_CHENGED_PATH + ") values (?,?,?,?,?)";
 
   public DataHelper(Context context) {
      this.context = context;
      OpenHelper openHelper = new OpenHelper(this.context);
      this.db = openHelper.getWritableDatabase();
      this.insertStmt = this.db.compileStatement(INSERT_SVN);
      this.insertStmt = this.db.compileStatement(INSERT_NOTIFI);
   };
   
	public boolean updateSvn(long rowId, String name, String url,
								String login, String pwd) {
		ContentValues updateValues = createContentValuesSvn(name, url, login, pwd );

		return db.update(SVN_TABLE, updateValues, ID + "="
				+ rowId, null) > 0;
	}

	public boolean deleteSvn(int id){
			db.delete(SVN_TABLE, "id= " +id, null);
		return true;
	}
	public boolean deleteNotifi(int id){
		db.delete(NOTIFI_TABLE, "id= " +id, null);
	return true;
}
	
	private ContentValues createContentValuesSvn(String name, String url, String login, String pwd) {
		ContentValues values = new ContentValues();
		values.put(SVN_NAME, name);
		values.put(SVN_URL, url);
		values.put(SVN_LOGIN, login);
		values.put(SVN_PASSWORD, pwd);
		return values;
	}

 
   public long insertSvn(String name, String url, String login, String pwd) {
	  this.insertStmt.bindString(1, name);
      this.insertStmt.bindString(2, url);
      this.insertStmt.bindString(3, login);
      this.insertStmt.bindString(4, pwd);
      return this.insertStmt.executeInsert();
   }
   public long insertNotifi( long revisionId, String author, String changedPaths, long date, String message) {
	      this.insertStmt.bindLong(1, revisionId);
	      this.insertStmt.bindString(2, author);
	      this.insertStmt.bindString(3, changedPaths);
	      this.insertStmt.bindLong(4, date);
	      this.insertStmt.bindString(5, message);
	      return this.insertStmt.executeInsert();
	   }
 
   public List<SVNRepo> selectAll() {
      List<SVNRepo> list = new ArrayList<SVNRepo>();
      Cursor cursor = this.db.query(SVN_TABLE, new String[] { SVN_NAME + " , " + SVN_URL + "," + SVN_LOGIN + "," + SVN_PASSWORD + " , " +ID}, 
        null, null, null, null, null);
      if (cursor.moveToFirst()) {
         do {
        	 SVNRepo sv = new SVNRepo();        	 
        	 sv.setName(cursor.getString(0));
        	 sv.setUrl(cursor.getString(1));
        	 sv.setLogin(cursor.getString(2));
        	 sv.setPwd(cursor.getString(3));
        	 sv.setId(cursor.getInt(4));
        	 list.add(sv); 
         } while (cursor.moveToNext());
      }
      if (cursor != null && !cursor.isClosed()) {
         cursor.close();
      }
      return list;
   }
 
   private static class OpenHelper extends SQLiteOpenHelper {
 
      OpenHelper(Context context) {
         super(context, SVNDB, null, DATABASE_VERSION);
      }

      public void onCreate(SQLiteDatabase db) {
         db.execSQL("CREATE TABLE " + SVN_TABLE 
        		 + "(id INTEGER PRIMARY KEY, " + SVN_NAME 
        		 + " TEXT, " + SVN_URL 
        		 + " TEXT, " + SVN_LOGIN 
        		 + " TEXT, " + SVN_PASSWORD + " TEXT)");
         db.execSQL("CREATE TABLE " + NOTIFI_TABLE 
        		 + "(id INTEGER PRIMARY KEY, " + NOTIFI_REV_ID 
        		 + " TEXT, " + NOTIFI_AUTHOR 
        		 + " TEXT, " + NOTIFI_REV_DATE
        		 + " TEXT, " + NOTIFI_MESSAGE
        		 + " TEXT, " + NOTIFI_CHENGED_PATH + " TEXT)");
      }
 
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
         Log.w("Example", "Upgrading database, this will drop tables and recreate.");
         db.execSQL("DROP TABLE IF EXISTS " + SVN_TABLE);
         db.execSQL("DROP TABLE IF EXISTS " + NOTIFI_TABLE);
         onCreate(db);
      }
   }
}